const path = require('path')
function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  // 如果你不需要生产环境的 source map，可以将其设置为 false 以加速生产环境构建。
  productionSourceMap: false,
  
  lintOnSave: false,
  
  // CSS 相关配置
  css: {
    // 是否使用css分离插件 ExtractTextPlugin
    extract: process.env.NODE_ENV === 'production',
    // 开启 CSS source maps?
    sourceMap: false,
    // css预设器配置项
    loaderOptions: {
      // 给 sass-loader 传递选项
      sass: {
        // 全局引入变量和 mixin
        additionalData: `@import "@/theme/variables.css";`
      },
      postcss: {
        postcssOptions: {
          plugins: [
            require('autoprefixer')({
              overrideBrowserslist: [
                '> 1%',
                'last 2 versions',
                'not dead',
                'not ie 11'
              ]
            })
          ]
        }
      }
    }
  },
  
  // webpack 配置
  configureWebpack: {
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    // 性能优化
    optimization: {
      splitChunks: {
        chunks: 'all',
        cacheGroups: {
          vendor: {
            name: 'chunk-vendors',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial'
          },
          ionic: {
            name: 'chunk-ionic',
            priority: 20,
            test: /[\\/]node_modules[\\/]@ionic[\\/]/
          }
        }
      }
    }
  },
  
  // PWA 配置
  pwa: {
    name: 'BookManager',
    themeColor: '#3880ff',
    msTileColor: '#000000',
    appleMobileWebAppCapable: 'yes',
    appleMobileWebAppStatusBarStyle: 'black',
    
    // 配置 workbox 插件
    workboxPluginMode: 'InjectManifest',
    workboxOptions: {
      swSrc: 'src/sw.js',
    }
  }
}